package com.infore.airsupervise.utils;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.MyApplication;
import com.infore.airsupervise.other.GlideRoundTransform;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.finalteam.galleryfinal.model.PhotoInfo;
import id.zelory.compressor.Compressor;

/**
 * 图片工具类
 *
 * @features: 图片的加载、压缩等
 * @author: Ding Xinlin
 * @date: 2016/12/28 15:46
 */

public class ImageUtil {

    private static ImageUtil instance;
    private String savePath = null;

    private ImageUtil() {
        File compressImgFolder = new File(Environment.getExternalStorageDirectory(), "/DCIM/" + "AirSupervise/Compress");
        if(!compressImgFolder.exists()) {
            compressImgFolder.mkdirs();
        }
        savePath = compressImgFolder.getAbsolutePath();
    }

    /**
     *  获取实例
     * @return
     */
    public static ImageUtil getInstance() {
        if (instance == null) {
            synchronized (ImageUtil.class) {
                if (instance == null) {
                    instance = new ImageUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 展示图片
     * @param imageView
     * @param uri
     * @param width
     * @param height
     */
    public void displayImage(ImageView imageView, String uri, int width, int height) {
        displayImage(imageView, uri, width, height, 0, R.drawable.ic_landscape_photo_blue, R.drawable.ic_landscape_photo_blue);
    }

    public void displayImage(
            ImageView imageView, String uri, int width, int height, int round, int placeholder, int errorImg) {
        Glide.with(imageView.getContext())
                .load(uri)
                .placeholder(placeholder)
                .error(errorImg)
                .override(width, height)
                .transform(new GlideRoundTransform(imageView.getContext(), round))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(imageView);
    }

    /**
     * 图片压缩
     * @param imgFile
     * @param maxW
     * @param maxH
     * @param quality
     * @return
     */
    public File compress(File imgFile, int maxW, int maxH, int quality) {
        File compressedImage = new Compressor.Builder(MyApplication.getInstance())
                .setMaxWidth(maxW)
                .setMaxHeight(maxH)
                .setQuality(quality)
                .setCompressFormat(Bitmap.CompressFormat.JPEG)
                .setDestinationDirectoryPath(savePath)
                .build()
                .compressToFile(imgFile);
        return compressedImage;
    }

    /*
     * 默认图片压缩方案
     */
    public File compressDefault( File imgFile) {
        return compress(imgFile, 720, 1280, 90);
    }

    /*
     * 图片上传时的压缩
     */
    public List<String> compressOnUpload(List<PhotoInfo> photos) {
        List<String> results = new ArrayList<>();
        for (PhotoInfo photo : photos) {
            File file = new File(photo.getPhotoPath());
            // 大于300k才压缩
            if (file.exists() && file.length() / 1024 > 300) {
                file = compressDefault(file);
                results.add(file.getAbsolutePath());
            } else {
                results.add(photo.getPhotoPath());
            }
            Log.d("Pictrue", "Size--->"+file.length()/1024);
        }
        return results;
    }

}
